package com.llc925.cst.myrecycleview_1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.llc925.cst.myrecycleview_1.R;
import com.llc925.cst.myrecycleview_1.bean.Result;
import com.llc925.cst.myrecycleview_1.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2017/4/16.
 */

public class RegisterActivity extends AppCompatActivity {
    private EditText userpass;
    private Button btn_reg;
    private Button btn_fh;
    private EditText username;
    private EditText mobilenum;
    private EditText address;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvents();
        initData();
    }

    //界面初始化
    public void initViews() {
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_fh = (Button) findViewById(R.id.btn_fh);
        userpass = (EditText) findViewById(R.id.userpass);
        username = (EditText) findViewById(R.id.username);
        mobilenum = (EditText) findViewById(R.id.mobilenum);
        address = (EditText) findViewById(R.id.address);
    }

    public void initEvents() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel = new UserModel();
                Call call = userModel.register(username.getText().toString(), userpass.getText().toString(),
                        mobilenum.getText().toString(), address.getText().toString());
                Callback<Result> callback = new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Result Result = response.body();
                        if (!"0".equals(Result.getSuccess())) {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {
                    }
                };
                call.enqueue(callback);
            }

            ;
        });
        btn_fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this, LoginActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    public void initData() {

    }
}
